/*
  backgrid-text-cell
  http://github.com/wyuenho/backgrid

  Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
  Licensed under the MIT @license.
*/
(function (root, factory) {

  // CommonJS
  if (typeof exports == "object") {
    module.exports = factory(require("underscore"),
                             require("backgrid"));
  }
  // Browser
  else factory(root._, root.Backgrid);

}(this, function (_, Backgrid)  {

  Backgrid.Extension.ButtonCell = Backgrid.Cell.extend({

    /** @property */
    className: "button-cell",
    
    render:function(){
    	var _this=this;
    	this.$el.empty();
	    var model = this.model;
	    
	    var btnJsons=eval( model.get(this.column.get("name")));
	    $(btnJsons).each(function(i,obj){
	    	var btn=$("<button class='btn btn-primary btn-xs'>"+obj.label+"</button>");
	    	
	    	//add click events
	    	var evtArgs='(';
	    	$(obj.args).each(function(i,obj){
	    		if(i>0){
	    			evtArgs+=',';
	    		};
	    		evtArgs+='\"'+model.get(obj.field)+'\"';
	    	});
	    	evtArgs+=')';
	    	var evtCall=obj.click+evtArgs;
	    	btn.attr('onclick',evtCall);
		    //btn.on('click',{id:model.get("id")},obj.click);
		    
				_this.$el.append(btn);
	    });
	    
	    this.delegateEvents();
	    return this;
    }

  });

}));
